package com.sam.jifenbao;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.sam.jifenbao.dialog.RedeemDialog;

/**
 * Created by Administrator on 2016/5/25.
 */
public class SB_CouponDetailActivity extends BaseActivity {
    private TextView couponValueTV,couponNameTV,couponRuleTV,couponValidTimeTV;
    private View suiteForShopBtn,couponDetailPanel;
    private Button useBtn;

    private View dialogLayout;
    private TextView redeemCostTV,confirmBtn,cancelBtn,errorMessageTV;
    private EditText verifyCodeET;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.coupon_detail_with_dialog);
        findView();
        initView();
    }

    @Override
    protected void findView() {
        couponValueTV = (TextView) findViewById(R.id.coupon_value);
        couponNameTV = (TextView) findViewById(R.id.coupon_name);
        couponRuleTV = (TextView) findViewById(R.id.coupon_rule);
        couponValidTimeTV  = (TextView) findViewById(R.id.coupon_valid_time);
        useBtn = (Button) findViewById(R.id.use_btn);
        suiteForShopBtn = findViewById(R.id.suite_for_shop_btn);
        couponDetailPanel = findViewById(R.id.coupon_detail_panel);

        //  dialog 部分
        dialogLayout = findViewById(R.id.dialog_layout);
        redeemCostTV = (TextView) findViewById(R.id.redeem_cost);
        confirmBtn = (TextView) findViewById(R.id.confirm_btn);
        cancelBtn = (TextView) findViewById(R.id.cancel_btn);
        verifyCodeET = (EditText) findViewById(R.id.verify_code_et);
        errorMessageTV = (TextView) findViewById(R.id.error_message);
    }

    @Override
    protected void initView() {
        couponDetailPanel.setEnabled(true);
        useBtn.setText(getString(R.string.use_right_now));
        useBtn.setEnabled(true);
        Intent intent = getIntent();
        try {
//            couponValueTV.setText(intent.getStringExtra(CouponListActivity.KEY_COUPON_VALUE));
//            couponNameTV.setText(intent.getStringExtra(CouponListActivity.KEY_COUPON_NAME));
//            couponRuleTV.setText(intent.getStringExtra(CouponListActivity.KEY_COUPON_RULE));
//            couponValidTimeTV.setText(intent.getStringExtra(CouponListActivity.KEY_COUPON_VALID_TIME));
        } catch (Exception e){
            Log.e("sam","在优惠券详情页面获取传过来的信息出错");
        }

        useBtn.setOnClickListener(onClickListener);
        suiteForShopBtn.setOnClickListener(onClickListener);
        confirmBtn.setOnClickListener(onClickListener);
        cancelBtn.setOnClickListener(onClickListener);
    }


    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.suite_for_shop_btn:
                    startActivity(new Intent(SB_CouponDetailActivity.this,ShopListActivity.class));
                    break;
                case R.id.use_btn:
                    dialogLayout.setVisibility(View.VISIBLE);
                    break;
                case R.id.confirm_btn:
                    String code = verifyCodeET.getText().toString();
                    if(TextUtils.isEmpty(code)) {
                        verifyCodeET.requestFocus();
                        errorMessageTV.setVisibility(View.GONE);
                    } else if(isValidVerifyCode(code)){
                        errorMessageTV.setVisibility(View.GONE);
                        couponDetailPanel.setEnabled(false);
                        useBtn.setText(getString(R.string.already_used));
                        useBtn.setEnabled(false);
                        startActivity(new Intent(SB_CouponDetailActivity.this, CouponUsedActivity.class));
                        dialogLayout.setVisibility(View.GONE);
                    } else {
                        errorMessageTV.setVisibility(View.VISIBLE);
                        verifyCodeET.requestFocus();
                    }
                    break;
                case R.id.cancel_btn:
                    Toast.makeText(SB_CouponDetailActivity.this,"取消对兑换",Toast.LENGTH_SHORT).show();
                    dialogLayout.setVisibility(View.GONE);
                    break;
            }
        }
    };


    private boolean isValidVerifyCode(String code){
        int value = Integer.parseInt(code);
        return value % 10 + value / 10 % 10 + value / 100 % 10 + value / 1000 % 10 == 20;
    }

}
